package com.hz.ruoyilgorder.rabbitmq;


import com.hz.ruoyilgorder.api.service.SubColdSome;
import com.hz.ruoyilgorder.websocket.MyWebSocketHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Component
public class MessageConsumer {

    @Autowired
    RedisTemplate<Object, Object> redisTemplate;
    @Autowired
    private SubColdSome subColdSome;
    // 监听队列中的消息
    @RabbitListener(queues = "canal.queue")
    public void consumeMessage(String message) {

        if (message.equals("1")){
//            System.out.println("Received message: " +"数据新增，修改redis");
            List list = subColdSome.getColdCount();
            redisTemplate.opsForValue().set("coldCount",list,1, TimeUnit.HOURS);
//            System.out.println("数据库更新成功，推送消息给前端...");
            MyWebSocketHandler.sendMessageToAll("数据已更新，请刷新页面！");

        }
        if (message.equals("0")){
           // System.out.println("Received message: " +"数据删除，修改redis");
            List list = subColdSome.getColdCount();
            redisTemplate.opsForValue().set("coldCount",list,1, TimeUnit.HOURS);
            //System.out.println("数据库更新成功，推送消息给前端...");
            MyWebSocketHandler.sendMessageToAll("数据已更新，请刷新页面！");
        }
        if (message.equals("2")){
            //System.out.println("Received message: " +"数据修改，修改redis");
            List list = subColdSome.getColdCount();
            redisTemplate.opsForValue().set("coldCount",list,1, TimeUnit.HOURS);
//            System.out.println("修改成功");
//            System.out.println("数据库更新成功，推送消息给前端...");
            System.out.println("修改成功");
            //System.out.println("数据库更新成功，推送消息给前端...");
            MyWebSocketHandler.sendMessageToAll("数据已更新，请刷新页面！");
        }
    }
}
